package com.lin.community.service;


import com.lin.community.dto.PageDTO;
import com.lin.community.dto.QuestionDTO;
import com.lin.community.exception.CustomizeErrorCode;
import com.lin.community.exception.CustomizeException;
import com.lin.community.mapper.QuestionMapper;
import com.lin.community.mapper.UserMapper;
import com.lin.community.model.Question;
import com.lin.community.model.QuestionExample;
import com.lin.community.model.User;
import org.apache.ibatis.session.RowBounds;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * (Question)表服务接口
 *
 * @author makejava
 * @since 2020-02-01 15:14:10
 */
@Service
public class QuestionService{

    @Autowired
    UserMapper userMapper;

    @Autowired
    QuestionMapper questionMapper;

    public PageDTO list(Integer page,Integer size){
        return getPageDTO(0,page,size);
    }

    public PageDTO listByUserId(Integer userId, Integer page, Integer size) {
        return getPageDTO(userId, page, size);
    }

    @NotNull
    private PageDTO getPageDTO(Integer key, Integer page, Integer size) {
        PageDTO pageDTO = new PageDTO();
        Integer totalCount;
        Integer totalPage;
        if (key!=0){
            QuestionExample questionExample = new QuestionExample();
            questionExample.createCriteria().andCreatorEqualTo(key);
            totalCount = (int)questionMapper.countByExample(questionExample);
        }else{
            totalCount = (int)questionMapper.countByExample(new QuestionExample());
        }
        if (totalCount % size == 0) {
            totalPage = totalCount / size;
        } else {
            totalPage = totalCount / size + 1;
        }
        if (page<1){
            page = 1;
        }
        if (page>totalPage){
            page =totalPage;
        }
        pageDTO.setPage(totalPage,page);

        Integer offset = size*(page-1);
        List<Question> questionList;
        if (key!=0){
            QuestionExample questionExample = new QuestionExample();
            questionExample.createCriteria().andCreatorEqualTo(key);
            questionList = questionMapper.selectByExampleWithRowbounds(questionExample,new RowBounds(offset,size));
        }else{
            questionList = questionMapper.selectByExampleWithRowbounds(new QuestionExample(),new RowBounds(offset,size));
        }
        List<QuestionDTO> questions = new ArrayList<>();

        for (Question question:questionList){
            User user = userMapper.selectByPrimaryKey(question.getCreator());
            QuestionDTO questionDTO = new QuestionDTO();
            BeanUtils.copyProperties(question,questionDTO);
            questionDTO.setUser(user);
            questions.add(questionDTO);
        }
        pageDTO.setQuestions(questions);

        return pageDTO;
    }

    public QuestionDTO getById(Integer id) {
        Question question = questionMapper.selectByPrimaryKey(id);
        if (question==null){
            throw new CustomizeException(CustomizeErrorCode.QUESTION_NOT_FIND);
        }
        User user = userMapper.selectByPrimaryKey(question.getCreator());
        QuestionDTO questionDTO = new QuestionDTO();
        BeanUtils.copyProperties(question,questionDTO);
        questionDTO.setUser(user);
        return questionDTO;
    }

    public void createOrUpdate(Question question) {
        if (question.getId()==null){
            question.setGmtCreate(System.currentTimeMillis());
            question.setGmtModified(question.getGmtCreate());
            questionMapper.insert(question);
        }else{
            question.setGmtModified(System.currentTimeMillis());
            Question updateQuestion = new Question();
            updateQuestion.setGmtModified(System.currentTimeMillis());
            updateQuestion.setDescription(question.getDescription());
            updateQuestion.setTags(question.getTags());
            updateQuestion.setTitle(question.getTitle());
            QuestionExample example = new QuestionExample();
            example.createCriteria()
                    .andIdEqualTo(question.getId());
            int updated = questionMapper.updateByExampleSelective(updateQuestion, example);
            if (updated!=1){
                throw new CustomizeException(CustomizeErrorCode.QUESTION_NOT_FIND);
            }
        }

    }
}